package util

import (
	"crypto/rand"
	"fmt"
	"io"
	"log"

	"github.com/sony/sonyflake"
)

func NewUUID() (string, error) {
	uuid := make([]byte, 16)
	n, err := io.ReadFull(rand.Reader, uuid)
	if n != len(uuid) || err != nil {
		return "", err
	}

	uuid[8] = uuid[8]&^0xc0 | 0x80
	uuid[6] = uuid[6]&^0xf0 | 0x40
	return fmt.Sprintf("%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:10], uuid[10:16]), nil
}

func GenSonyFlake() uint64 {
	flake := sonyflake.NewSonyflake(sonyflake.Settings{})
	id, err := flake.NextID()
	if err != nil {
		log.Fatalf("flase.NextID() failed with %s\n", err)
	}
	return id
}
